/**
 * Created by zycool on 2016/7/26.
 * 统计 - 维修整改 [从管体缺陷移植过来]
 */


;(function ($, window, document, undefined) {

    // 缺陷维修ID
    var taskid = $('#JQ_taskid').val();
    var url = DyyBase.root_path + '/corrctrl/repairMgr/statistics.json?repairTaskId=' + taskid;
    DyyBase.ajax(url, {}, function (dataset) {
        if (dataset.bool) {
            if (dataset.result) {
                var result = DyyBase.clearNull(dataset.result);
                for (var o in result) {
                    $('#' + o).text(result[o]).prop('title', result[o]);
                }
            }

            var defectInfo = dataset.result.defectStatistics;
            var repairStateList = dataset.result.repairStatisticsList;

            var total = parseInt(defectInfo.alreadyRepair) + parseInt(defectInfo.waitForRepair) + parseInt(defectInfo.maintenance) + parseInt(defectInfo.difficultToRepair);

            var p = '<p>'
                + '<span style="width: 20%">' + total + '</span>'
                + '<span style="width: 20%">' + defectInfo.alreadyRepair + '</span>'
                + '<span style="width: 20%">' + defectInfo.waitForRepair + '</span>'
                + '<span style="width: 20%">' + defectInfo.maintenance + '</span>'
                + '<span style="width: 20%">' + defectInfo.difficultToRepair + '</span>'
                + '</p>';
            $('#JQ_statics_body').html(p);

            // 绘图
            var data = d3Charts.formart(repairStateList,{x:'name',y:'val'});

            var temp = [{x:'default',y:0}];
            var temp_arr = temp.concat(data);
            temp_arr.push({x:'default',y:0});

            d3Charts.bar({
                container: 'JQ_echarts',
                color: ['#61ade9'],
                data: temp_arr,
                xAxis: {
                    data: [],
                    ticks: 0,
                    show: true,
                    render: function(d){
                        if(d === 'default'){
                            return '';
                        }else{
                            return d;
                        }
                    }
                },
                yAxis: {
                    data: [0,total],
                    ticks: total,
                    show: true,
                    render: function(d){
                        return d;
                    }
                },
                layout: {
                    xtag: '',
                    ytag: '缺陷数量',
                    margin: {
                        left: 30,
                        right: 30,
                        top: 30,
                        bottom: 30
                    },
                    xgrid: true,
                    ygrid: true
                },
                tip: {
                    show: true,
                    xtag: '',
                    ytag: '数量'
                }
            });
        }
    }, {type: 'GET'});

    // 返回
    $('#JQ_statistics_goback').click(function () {
        window.location.href = DyyBase.root_path + '/corrctrl/CoatRepairTask/init.html';
    });

    // 导出
    $('#JQ_statistics_export').click(function () {
        var $this = $(this);
        DyyBase.isLogin(function(){
            var pipeName = $('#pipeLineName').text();
            var projectName = $('#repairTaskName').text();
            $this.EXPT({
                type: 'name',
                name: pipeName + projectName + '漏损点维修统计图',
                yesBtnAct: function(callback,fname){
                    callback();
                    canvasPdf.exportHtmlPdf({
                        dom: $('#JQ_pdf_area'),
                        server: DyyBase.root_path + '/comm/downImgPdf.json',
                        param: {
                            fileName: fname,
                            name: projectName + '漏损点维修统计图'
                        }
                    });
                }
            });
        });
    });

})(jQuery, window, document);
